package com.uenpay.im.ui.old.activity.red;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.aswife.ui.RoundedImageView;
import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.utils.Utils;
import com.qiyunxin.android.http.view.ResizeLayout;
import com.qyx.android.message.type.PacketType;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.wallet.MyRedPagerActivity;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.panel.FacePanelByEmojiManager;
import com.uenpay.im.panel.PanelManagerInterface;
import com.uenpay.util.app.base.BaseActivity;

import java.text.MessageFormat;

/**
 * @author SL
 * @created 2016-1-15 11:28:41
 * @copyRight http://www.qiyunxin.com
 * @function 红包详情
 */
public class RedDetailActivity extends BaseActivity {

    private RoundedImageView imageView;
    private TextView send_red_money_tv, send_red_content_tv, send_red_name_tv,
            red_total_money_tv, spendtime_tv, is_save_tv;
    private ReceiveRedEntity receiveRedEntity = null;
    private ListView listView;
    private RedDetailAdapter mAdapter;
    private TextView check_my_red_record_tv;
    private LinearLayout wait_layout, money_layout;
    private FacePanelByEmojiManager mFacePanelByEmojiManager;
    private ResizeLayout mResizeLayout;
    private View dynamic_pop_reply;
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_red_detail_layout;
    }

    @Override
    public void initView(View view) {
        receiveRedEntity = (ReceiveRedEntity) getIntent().getSerializableExtra(
                "mReceiveRedEntity");
        initView();
        initListener();
        initFacePanelView();
    }

    protected void initView() {
        findViewById(R.id.pic_switch_btn).setVisibility(View.GONE);
        editText = (EditText) findViewById(R.id.editText);
        dynamic_pop_reply = findViewById(R.id.dynamic_pop_reply);
        mResizeLayout = (ResizeLayout) findViewById(R.id.parent);
        listView = (ListView) findViewById(R.id.listview);

        View view = View.inflate(this, R.layout.red_detail_header_layout, null);
        is_save_tv = (TextView) view.findViewById(R.id.is_save_tv);
        wait_layout = (LinearLayout) view.findViewById(R.id.wait_layout);
        red_total_money_tv = (TextView) view.findViewById(R.id.red_total_money_tv);
        spendtime_tv = (TextView) view.findViewById(R.id.spendtime_tv);
        send_red_money_tv = (TextView) view.findViewById(R.id.send_red_money_tv);
        money_layout = (LinearLayout) view.findViewById(R.id.money_layout);
        imageView = (RoundedImageView) view.findViewById(R.id.user_iv);
        send_red_name_tv = (TextView) view.findViewById(R.id.send_red_name_tv);
        send_red_content_tv = (TextView) view.findViewById(R.id.send_red_content_tv);
        listView.addHeaderView(view);
        View footerView = View.inflate(this, R.layout.red_detail_footer_layout, null);
        check_my_red_record_tv = (TextView) footerView.findViewById(R.id.check_my_red_record_tv);
        setData();
        listView.addFooterView(footerView);
    }

    protected void initListener() {
        //返回键
        findViewById(R.id.back_layout).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });
        check_my_red_record_tv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(RedDetailActivity.this,
                        ReceviedOrSendRedActivity.class);
                intent.putExtra("type", 1);
                startActivity(intent);
            }
        });
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {

                String content = editText.getText().toString();
                if (Utils.String_length(content) > 50) {
                    if (content.length() > 25) {
                        editText.setText(content.substring(0, 25));
                        editText.setSelection(editText.getText().toString()
                                .length());
                    }
                    return;
                }
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                                          int arg2, int arg3) {

            }

            @Override
            public void afterTextChanged(Editable arg0) {

            }
        });

        is_save_tv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(RedDetailActivity.this, MyRedPagerActivity.class);
                startActivity(intent);
            }
        });
        listView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (mFacePanelByEmojiManager != null) {
                    mFacePanelByEmojiManager.hideFacePanelView();
                }
                dynamic_pop_reply.setVisibility(View.GONE);
                return false;
            }
        });
    }

    private void setData() {
        if (receiveRedEntity != null) {
            send_red_name_tv.setText(MessageFormat.format(getResources()
                            .getString(R.string.red_who_send),
                    receiveRedEntity.nickName));
            if (TextUtils.isEmpty(receiveRedEntity.blessing)) {
                send_red_content_tv.setText(R.string.red_default_content);
            } else {
                send_red_content_tv.setText(receiveRedEntity.blessing);
            }
            send_red_money_tv.setText(receiveRedEntity.amount);
            imageView.SetUrl(APIConfiguration.getAvatarUrlNormal(
                    receiveRedEntity.custId, PacketType.PERSONAL));
            if (receiveRedEntity.custId.equals(IMApplication.getCustId())) {
//				red_total_money_tv.setText(MessageFormat.format(getResources()
//						.getString(R.string.red_total_count),
//						receiveRedEntity.number, receiveRedEntity.amount));
                wait_layout.setVisibility(View.VISIBLE);
            } else {
//				red_total_money_tv.setText(MessageFormat.format(getResources()
//						.getString(R.string.red_received_count),
//						receiveRedEntity.opennumber, receiveRedEntity.number));
                wait_layout.setVisibility(View.VISIBLE);
                check_my_red_record_tv.setVisibility(View.VISIBLE);
            }
            red_total_money_tv.setText(MessageFormat.format(getResources()
                            .getString(R.string.red_received_count),
                    receiveRedEntity.opennumber, receiveRedEntity.number));

            if (receiveRedEntity.custId.equals(IMApplication.getCustId())) {
                check_my_red_record_tv.setVisibility(View.VISIBLE);
                check_my_red_record_tv.setText(R.string.return_red_envelope);
                check_my_red_record_tv.setTextColor(getResources().getColor(R.color.the_color_pale_gray));
            } else {
                check_my_red_record_tv.setVisibility(View.VISIBLE);
            }

            if (!TextUtils.isEmpty(receiveRedEntity.pickAmount)) {
                double pickAmount = Double.valueOf(receiveRedEntity.pickAmount);
                if (pickAmount > 0) {
                    send_red_money_tv.setText(receiveRedEntity.pickAmount);
                    money_layout.setVisibility(View.VISIBLE);
                    is_save_tv.setVisibility(View.VISIBLE);
                }
            }

            if (receiveRedEntity.leftPacketsNum == 0
                    && receiveRedEntity.spendtime > 0) {
                int h = 0;
                int d = 0;
                int s = 0;
                int temp = (int) (receiveRedEntity.spendtime % 3600);
                if (receiveRedEntity.spendtime > 3600) {
                    h = (int) (receiveRedEntity.spendtime / 3600);
                    if (temp != 0) {
                        if (temp > 60) {
                            d = temp / 60;
                            if (temp % 60 != 0) {
                                s = temp % 60;
                            }
                        } else {
                            s = temp;
                        }
                    }
                } else {
                    d = (int) (receiveRedEntity.spendtime / 60);
                    if (receiveRedEntity.spendtime % 60 != 0) {
                        s = (int) (receiveRedEntity.spendtime % 60);
                    }
                }
                if (d == 0) {
                    spendtime_tv.setText(MessageFormat.format(getResources()
                                    .getString(R.string.spendtime_second),
                            receiveRedEntity.spendtime));
                } else if (d > 0 && h == 0) {
                    spendtime_tv.setText(MessageFormat.format(getResources()
                            .getString(R.string.spendtime_minute), d, s));
                } else if (h > 0) {
                    spendtime_tv.setText(MessageFormat.format(getResources()
                            .getString(R.string.spendtime_hour), h, d, s));
                }
            } else {
                if (receiveRedEntity.custId.equals(IMApplication
                        .getCustId())) {
                    // TODO
                    spendtime_tv.setText(MessageFormat.format(
                            getResources().getString(R.string.open_amount),
                            receiveRedEntity.openamount,
                            receiveRedEntity.amount));
                }
            }

            mAdapter = new RedDetailAdapter(RedDetailActivity.this,
                    receiveRedEntity.arrayList, new RedDetailAdapter.ITanksRed() {

                @Override
                public void onTanksRed() {
                    showFace();
                }
            });
            listView.setAdapter(mAdapter);
        }
    }

    private void thanksRed(String packetsid, String thankswords) {
        RedHandler.returnTanksRed(packetsid, thankswords,
                new RedHandler.ITanksRedListener() {

                    @Override
                    public void onResult(int status, String msg) {
                        if (status == HttpInvokeResultCode.RESULT_OK) {
                            getRedDeatil();
                        } else if (!TextUtils.isEmpty(msg)) {
                            IMApplication.showToast(msg);
                        }
                    }
                });
    }

    private void initFacePanelView() {
        mFacePanelByEmojiManager = new FacePanelByEmojiManager(this,
                dynamic_pop_reply, mResizeLayout);
        mFacePanelByEmojiManager.initListener();
        mFacePanelByEmojiManager.onSendTextListener(new PanelManagerInterface.ISendTextListener() {

            @Override
            public void onSendTextContent(String editContent) {
                if (!TextUtils.isEmpty(editContent.trim())) {
                    if (editContent.length() <= 140) {
                        Utils.hideSoftKeyboard(editText);
                        dynamic_pop_reply.setVisibility(View.GONE);
                        thanksRed(receiveRedEntity.packetsid, editContent);
                    } else {
                        IMApplication.showToast(R.string.more_text_max);
                    }
                } else {
                    IMApplication.showToast(R.string.reply_empty);
                }
            }
        });
    }

    private void showFace() {
        dynamic_pop_reply.setVisibility(View.VISIBLE);
        if (mFacePanelByEmojiManager != null) {
            mFacePanelByEmojiManager.showFacePanelView();
            Utils.showSoftKeyboard(editText);
        }
    }

    private void getRedDeatil() {
        RedHandler.pickRed(receiveRedEntity.packetsid, new RedHandler.IPickRedListener() {

            @Override
            public void onPickRed(int status, String msg,
                                  ReceiveRedEntity mReceiveRedEntity) {
                if (status == HttpInvokeResultCode.RESULT_OK) {
                    receiveRedEntity = mReceiveRedEntity;
                    setData();
                }
            }
        });
    }

}
